﻿using System.Globalization;
using System.Windows.Forms;
using Scommesse.Model;
using Scommesse.Persistence;
using Scommesse.Presenters.Adapters;
using Scommesse.View;
using System.Drawing;
using System.Collections.Generic;

namespace Scommesse.Presenters
{
    public class ScommessePresenter : IScommessePresenter
    {
        private readonly BetCard _betCard;
        private readonly ScommesseForm _scommessaForm;

        public ScommessePresenter(BetCard betCard)
        {
            _betCard = betCard;
            _scommessaForm = new ScommesseForm();
            _scommessaForm.AddListener(this);
            _scommessaForm.ShowDialog();
        }

        #region IScommessePresenter Members

        public void LoadDatiPersonali()
        {
            _scommessaForm.Credito = _betCard.Credito.ToString("c", new CultureInfo("it-IT"));
            _scommessaForm.Codice = _betCard.CodiceTessera;
            _scommessaForm.NomeCognome = _betCard.Cliente.Nome + " " + _betCard.Cliente.Cognome;
        }
        public void Populate(TreeNodeCollection nodes, IEnumerable<IEntity> entities, int level)
        {
            if (level == 0)
                return;
            
            foreach (IEntity entity in entities)
            {
                TreeNode node = new TreeNode(entity.FullName);
                node.BackColor = entity.BackColor;
                node.ForeColor = entity.ForeColor;
                if (entity.Font != null)
                {
                    node.NodeFont = entity.Font;
                }
                nodes.Add(node);
                if(entity is IComposite)
                Populate(node.Nodes, ((IComposite)entity).SubEntities, level - 1);
            }
        }

        public void PopulateListView()
        {
            _scommessaForm.ScommesseTreeView.Nodes.Clear();
            List<IEntity> entities = new List<IEntity>();
            foreach (ScommessaController scommessaController in _betCard.Scommesse)
            {
                ScommessaConfermata s = (ScommessaConfermata)scommessaController.Scommessa;
                if (s.Controllabile())
                 scommessaController.ControllaScommessa();
                if (s.Stato.Equals(StatoScommessaConfermata.Vinta))
                {
                    entities.Add(new ScommessaAdapter(s,Color.LightGreen,Color.Empty,null));
                }
                else if (s.Stato.Equals(StatoScommessaConfermata.Persa))
                {
                    entities.Add(new ScommessaAdapter(s, Color.OrangeRed, Color.Empty, null));
                }
                else
                {
                    entities.Add(new ScommessaAdapter(s));
                }
            }
            Populate(_scommessaForm.ScommesseTreeView.Nodes,entities,2);
            _scommessaForm.ScommesseTreeView.Sort();
            _scommessaForm.ScommesseTreeView.ExpandAll();
           
            // Salvo la BetCard perchè il credito potrebbe essere stato modificato dai pagamenti delle scommesse vinte
            DataSaver.SaveBetCardOnFile(_betCard);
            _scommessaForm.ScommesseTreeView.HideSelection = true;
        }
    }
        #endregion
}